#include "BitmapLoader.h"
#include <stdio.h>
#include <iostream>
#include "edge_detection.h"

using namespace std;

void main()
{
	// NOTE: our code assumes a 24-bit BMP file is being loaded (RGB no compression)
	Bitmap original;
	Bitmap* result;
	char buf[128];
	char buf2[128];
	unsigned char* processedRGB;
	unsigned char* d_rgb;
	unsigned char* d_out;

	printf("Enter file path: ");
	cin.getline(buf, 128);

	void* ferror = original.LoadBitmap(buf);

	if (ferror == 0)
	{
		printf("Unable to open file %s\n", buf);
		getchar();
		return;
	}

	sprintf(buf2, "result_%s", buf);
	// allocate data for result
	result = new Bitmap(original);

	processedRGB = new unsigned char[original.height * original.width*3];
	memset(processedRGB, 0, original.height*original.width*3);
	//printf("%u\n", processedRGB[0]);
	// allocate space on CUDA device for image and copy in original data
	cudaMalloc((void**)&d_rgb, original.height * original.width*3);
	cudaMalloc((void**)&d_out, original.height * original.width *3);
	cudaMemcpy((void*)d_rgb, (void*)original.bitmap_Image, original.height*original.width*3, cudaMemcpyHostToDevice);
	cudaMemcpy((void*)d_out, (void*)processedRGB, original.height*original.width*3, cudaMemcpyHostToDevice);

	// set thread count
	dim3 NumBlocks (original.height, original.width, 1);
	dim3 NumThreads (1,1,1);
	
	EdgeDetectDevice(NumBlocks, NumThreads, d_rgb, d_out);

	cudaMemcpy((void*)processedRGB, (void*)d_out, original.height*original.width*3, cudaMemcpyDeviceToHost);
	//printf("%u\n", processedRGB[0]);
	result->bitmap_Image = processedRGB;
	result->flush(buf2);
}